package com.dwd.quartz.service.impl;

import com.dwd.basic.constant.PayConstants;
import com.dwd.order.pojo.ProductOrder;
import com.dwd.quartz.job.OrderJob;
import com.dwd.quartz.pojo.QuartzJobInfo;
import com.dwd.quartz.service.IQuartzService;
import com.dwd.quartz.util.QuartzUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/30 10:58
 * @Version 1.0
 */
@Service
public class QuartzServiceImpl implements IQuartzService {

    /**
     * 用来获取schedule对象
     */
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    /**
     * 添加任务
     *
     * @param quartzJobInfo
     */
    @Override
    public void addJob(QuartzJobInfo quartzJobInfo) {
        QuartzUtils.addJob(schedulerFactoryBean.getObject(), quartzJobInfo.getJobName(),
                OrderJob.class, quartzJobInfo, quartzJobInfo.getCronj());
    }

    @Override
    public void remove(ProductOrder productOrder) {
        QuartzUtils.removeJob(schedulerFactoryBean.getObject(), PayConstants.BUSINESSTYPE_PRODUCT + productOrder.getOrderSn());
    }
}
